import { router } from 'umi'
import * as shopApi from '@/services/order'
import { message } from 'antd'
import {billRefundOrder, closeOrder, saveOrUpdateOrderCustomerAttendance, updateOrderStagesById} from '@/services/order'

export default {
  namespace: 'order',
  state: {
    allList: [], // 全部列表
    allTotal: 1,
    allListCount: [], // 全部订单类型数据
    allListExt: [], // 全部列表带详情
    allTotalExt: 1,
    closeList: [], // 关闭列表
    closeTotal: 1,
    conditionList: [], // 逾期列表列表
    conditionTotal: 1,
    BuyOutList: [], // 买断订单列表
    BuyOutTotal: 1,
    ReletList: [], // 查询续租订单
    ReletTotal: 1,
    AuditList: [], // 电审
    AuditTotal: 1,
    PlateformList: [], // 获取上架渠道集合
    userOrderInfoDto: {}, // 订单信息
    shopInfoDto: {}, // 商家信息
    orderAddressDto: {}, // 收货人信息
    receiptExpressInfo: {}, // 发货物流信息
    giveBackExpressInfo: {}, // 归还物流信息
    productInfo: [],
    opeRemarkDtoPage: {}, // 平台备注
    // OrderRemarkDataTotal: 1,
    businessRemarkDtoPage: {}, // 商家备注
    // businessRemarkDtoTotal: 1,
    userOrderCashesDto: [], // 账单信息
    orderByStagesDtoList: [], // 分期信息
    orderBuyOutDto: [], // 买断信息
    wlList: [], // 物流信息
    originalOrderDto: [], // 原定单详情
    userOrderBuyOutDto: [], // 买断信息
    userOrderPurchaseList: [],
    userOrderPurchaseTotal: 1
  },
  effects: {
    *queryOpeOrderByConditionList({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOpeOrderByCondition, payload)

      yield put({
        type: 'allListData',
        payload: response
      })
    },
    *queryOrderGroupCountByCondition({ payload, callback }, { call, put }) {
      const response = yield call(
        shopApi.queryOrderGroupCountByCondition,
        payload
      )

      yield put({
        type: 'allListCount',
        payload: response
      })
    },
    *rentOrderPageList({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.rentOrderPageList, payload)
      yield put({
        type: 'allListData',
        payload: response
      })
    },
    // 查询所有订单带详情
    *queryOrderByConditionExt({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOrderByConditionExt, payload)

      yield put({
        type: 'allListDataExt',
        payload: response
      })
    },
    *applyCancel({ payload, callback }, { call, put }) {
      const res = yield call(shopApi.applyCancel, {
        ...payload
      })
      if (res.data) {
        callback && callback()
      } else {
        res.errorMessage &&
          Taro.showToast({
            title: res.errorMessage,
            icon: 'none'
          })
      }
    },
    *queryPendingOrderClosureList({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryPendingOrderClosureList, payload)

      yield put({
        type: 'closeListData',
        payload: response
      })
    },
    *closeUserOrderAndRefundPrice({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.closeUserOrderAndRefundPrice, payload)
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *queryOpeOverDueOrdersByCondition({ payload, callback }, { call, put }) {
      const response = yield call(
        shopApi.queryOpeOverDueOrdersByCondition,
        payload
      )

      yield put({
        type: 'conditionListData',
        payload: response
      })
    },
    *queryOpeGoingOverDueOrdersByCondition(
      { payload, callback },
      { call, put }
    ) {
      const response = yield call(
        shopApi.queryOpeGoingOverDueOrdersByCondition,
        payload
      )

      yield put({
        type: 'conditionListData',
        payload: response
      })
    },
    *queryWaitingGiveBackOrder({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryWaitingGiveBackOrder, payload)

      yield put({
        type: 'waitingGiveBackListData',
        payload: response
      })
    },
    *queryOpeBuyOutOrdersByCondition({ payload, callback }, { call, put }) {
      const response = yield call(
        shopApi.queryOpeBuyOutOrdersByCondition,
        payload
      )

      yield put({
        type: 'BuyOutListData',
        payload: response
      })
    },
    *businessOrderStatisticsUsing({ payload, callback }, { put, call }) {
      const response = yield call(shopApi.businessOrderStatisticsUsing, payload)
      if (response.responseType === 'SUCCESS') {
        callback && callback(response.data)
      }
    },
    *queryTelephoneAuditOrder({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryTelephoneAuditOrder, payload)

      yield put({
        type: 'AuditListData',
        payload: response
      })
    },
    *queryReletOrderByCondition({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryReletOrderByCondition, payload)

      yield put({
        type: 'ReletListData',
        payload: response
      })
    },
    *PlateformChannel({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.PlateformChannel, payload)
      yield put({
        type: 'PlateformData',
        payload: response
      })
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *orderRemark({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.orderRemark, payload)

      // message.warning('备注成功～');
      message.warning('备注成功～手动刷新页面可查看最新备注')
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *saveOrUpdateOrderCustomerAttendance({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.saveOrUpdateOrderCustomerAttendance, payload)
      message.warning('备注成功～')
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *purchaseOrder({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.purchaseOrder, payload)
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *queryUserWeixinPage({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryUserWeixinPage, payload)
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *queryOrderStagesDetail({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOrderStagesDetail, payload)
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
//套餐变更查询接口
    *selectProductByProducetId({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.selectProductByProducetId, payload)
      if (callback) callback(res)
    },
    //套餐修改接口
    *changeProductForOrder({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.changeProductForOrder, payload)
      message.warning('套餐变更成功～');
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *modifyUserWeixin({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.modifyUserWeixin, payload)
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *deleteUserWeixin({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.deleteUserWeixin, payload)
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *saveWeixinUserInfo({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.saveWeixinUserInfo, payload)
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *userOrderPurchase({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.userOrderPurchase, payload)

      yield put({
        type: 'userOrderPurchaseData',
        payload: response
      })
    },
    *telephoneAuditOrder({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.telephoneAuditOrder, payload)
      message.warning('备注成功～')
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },

    *queryOpeUserOrderDetail({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOpeUserOrderDetail, payload)
      yield put({
        type: 'OpeUserData',
        payload: response
      })
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *queryOpeBuyOutOrderDetail({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOpeBuyOutOrderDetail, payload)
      yield put({
        type: 'OpeUserData',
        payload: response
      })
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },

    // 续租
    *queryUserReletOrderDetail({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryUserReletOrderDetail, payload)
      yield put({
        type: 'OpeUserData',
        payload: response
      })
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *queryOrderRemark({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryOrderRemark, payload)
      console.log(payload.source)
      yield put({
        type: 'OrderRemarkData',
        payload: {
          response,
          source: payload.source
        }
      })
    },
    *queryExpressInfo({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.queryExpressInfo, payload)
      yield put({
        type: 'queryInfoList',
        payload: response
      })
    },
    *contractReport({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.contractReport, payload)
      window.open(response.data)
    },
    *selectDistrict({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.selectDistrict, payload)
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *opeOrderAddressModify({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.opeOrderAddressModify, payload)
      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *exportOpeAllUserOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportOpeAllUserOrders, payload)
      location.href = response.data
      message.success('导出成功～')
    },
    *exportQueryOrderByConditionExt({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportQueryOrderByConditionExt, payload)
      location.href = response.data
      message.success('导出成功～')
    },
    *userOrdersPurchase({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.userOrdersPurchase, payload)

      if (callback && typeof callback === 'function') {
        callback(response)
      }
    },
    *exportOpeBuyOutUserOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportOpeBuyOutUserOrders, payload)
      location.href = response.data
      message.success('导出成功～')
    },
    *exportOpeOverDueUserOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportOpeOverDueUserOrders, payload)
      // location.href = response.data
      // message.success('导出成功～')

      return response
    },
    *exportWaitingGiveBack({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportWaitingGiveBack, payload)
      location.href = response.data
      message.success('导出成功～')
    },
    *exportReletOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportReletOrders, payload)
      location.href = response.data
      message.success('导出成功～')
    },
    *exportTelephoneAuditOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportTelephoneAuditOrders, payload)
      location.href = response.data
      message.success('导出成功～')
    },
    *exportCloseRefundOrders({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.exportCloseRefundOrders, payload)
      location.href = response.data
      message.success('导出成功～')
    },
    // 获取信用详情
    *getCredit({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.getSiriusReportRecord, payload)
      if (callback) callback(res)
    },
    *getCreditV3({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.getSiriusReportRecordV3, payload)
      if (callback) callback(res)
    },
    *purchaseOrderDelivery({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.purchaseOrderDelivery, payload)
      if (callback) callback(res)
    },
    *purchaseOrderExport({ payload, callback }, { call, put }) {
      const response = yield call(shopApi.purchaseOrderExport, payload)
      location.href = response.data
      message.success('导出成功～')
    },
    // 获取蚂蚁链信息
    *getChainInfoByOrderId({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.getChainInfoByOrderId, payload)
      if (callback) callback(res)
    },
    // 查询蚁盾分
    *getUserRisk({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.getUserRisk, payload)
      if (callback) callback(res)
    },
    // 手动扣除分期账单--代扣
    *paidPeriodOrder({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.paidPeriodOrder, payload)
      if (callback) callback(res)
    },
    // 手动扣除分期账单--代扣
    *orderByStagesPay({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.orderByStagesPay, payload)
      if (callback) callback(res)
    },
    // 手动扣除分期账单--退款
    *billRefundOrder({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.billRefundOrder, payload)
      if (callback) callback(res)
    },
    // 增值服务--碎屏险--代扣
    *orderServicePay({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.orderServicePay, payload)
      if (callback) callback(res)
    },
    // 增值服务--碎屏险--退款
    *orderServiceRefund({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.orderServiceRefund, payload)
      if (callback) callback(res)
    },
    // 订单账单信息分期信息修改价格
    *updateOrderStagesById({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.updateOrderStagesById, payload)
      if (callback) callback(res)
    },
    // 订单账单信息修改增值服务价格
    *updatePriceByOrderAdditionalServicesId({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.updatePriceByOrderAdditionalServicesId, payload)
      if (callback) callback(res)
    },
    // 待归还状态下手动关单
    *finishedOrder({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.finishedOrder, payload)
      if (callback) callback(res)
    },
    // 修改订单押金
    *modifyOrderDeposit({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.modifyOrderDeposit, payload)
      callback && callback(res)
    },

    // 重新打印
    *sfRepeatPrintExpress({ payload, callback }, { put, call }) {
      const res = yield call(shopApi.sfRepeatPrintExpress, payload)
      if (res.errorCode == '-1') {
        message.error(res.errorMessage)
      } else {
        message.success('重新打印成功')
      }
      
      // callback && callback(res)
    }
  },
  reducers: {
    allListData(state, { payload }) {
      return {
        ...state,
        allList: payload.data.records,
        allTotal: payload.data.total
      }
    },
    // 带详情数据
    allListDataExt(state, { payload }) {
      return {
        ...state,
        allListExt: payload.data.records,
        allTotalExt: payload.data.total
      }
    },
    // 带详情数据
    allListCount(state, { payload }) {
      return {
        ...state,
        allListCount: payload.data
      }
    },
    closeListData(state, { payload }) {
      return {
        ...state,
        closeList: payload.data.records,
        closeTotal: payload.data.total
      }
    },
    userOrderPurchaseData(state, { payload }) {
      return {
        ...state,
        userOrderPurchaseList: payload.data.records,
        userOrderPurchaseTotal: payload.data.total
      }
    },
    conditionListData(state, { payload }) {
      return {
        ...state,
        conditionList: payload.data.records,
        conditionTotal: payload.data.total
      }
    },
    waitingGiveBackListData(state, { payload }) {
      return {
        ...state,
        waitingGiveBackList: payload.data.records,
        waitingGiveTotal: payload.data.total
      }
    },
    AuditListData(state, { payload }) {
      return {
        ...state,
        AuditList: payload.data.records,
        AuditTotal: payload.data.total
      }
    },
    BuyOutListData(state, { payload }) {
      return {
        ...state,
        BuyOutList: payload.data.records,
        BuyOutTotal: payload.data.total
      }
    },
    ReletListData(state, { payload }) {
      return {
        ...state,
        ReletList: payload.data.records,
        ReletTotal: payload.data.total
      }
    },
    PlateformData(state, { payload }) {
      return {
        ...state,
        PlateformList: payload.data
      }
    },
    OpeUserData(state, { payload }) {
      console.log('payload:', payload)
      return {
        ...state,
        ...payload.data,
        productInfo: [payload.data.productInfo],
        // OrderRemarkDataList: payload.data.opeRemarkDtoPage.records,
        // OrderRemarkDataTotal: payload.data.opeRemarkDtoPage.total,
        // businessRemarkDtoList: payload.data.businessRemarkDtoPage.records,
        // businessRemarkDtoTotal: payload.data.businessRemarkDtoPage.total,
        userOrderCashesDto: [payload.data.userOrderCashesDto],
        orderByStagesDtoList: payload.data.orderByStagesDtoList,
        orderBuyOutDto: [payload.data.orderBuyOutDto],
        originalOrderDto: [payload.data.originalOrderDto],
        userOrderBuyOutDto: [payload.data.userOrderBuyOutDto]
      }
    },
    OrderRemarkData(state, { payload }) {
      const { source, response } = payload
      if (source === '01') {
        return {
          ...state,
          opeRemarkDtoPage: response.data
        }
      }
      return {
        ...state,
        businessRemarkDtoPage: response.data
      }
    },
    queryInfoList(state, { payload }) {
      return {
        ...state,
        wlList: payload.data.result.list
      }
    }
  }
}
